package lumis.doui;

import lumis.portal.PortalException;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.serviceinstance.ServiceInstanceConfig;
import lumis.portal.stability.StableMinor;
import lumis.portal.user.UserConfig;
import lumis.util.PortalUtil;

import java.util.Date;

/**
 * Handles doui standard values.
 * 
 * @version $Revision: 13082 $ $Date: 2011-05-27 16:03:12 -0300 (Fri, 27 May 2011) $
 * @since 4.0.4
 */
@StableMinor(version = "6.0", sinceVersion = "4.0")
public class DouiStandardValue
{
	/**
	 * Converts lumis standard default values.
	 * @param douiContext the doui context where the value is used.
	 * @param value the lumis standard default value.
	 * @return the real value.
	 * @throws PortalException
	 * @since 4.0.4
	 */
	public static Object convertStandardValue(DouiContext douiContext, Object value) throws PortalException
	{
		Object resultValue = value;

		if (resultValue.equals("lum_currentChannel"))
		{
			resultValue = douiContext.getRequest().getPageConfig().getChannelId();
		}
		else if (resultValue.equals("lum_currentPage"))
		{
			resultValue = douiContext.getRequest().getPageConfig().getId();
		}
		else if (resultValue.equals("lum_currentServiceInstanceId"))
		{
			ServiceInstanceConfig serviceInstanceConfig = douiContext.getRequest().getServiceInstanceConfig();
			resultValue = serviceInstanceConfig != null ? serviceInstanceConfig.getId() : null;
		}
		else if (resultValue.equals("lum_currentServiceInstanceChannelId"))
		{
			ServiceInstanceConfig serviceInstanceConfig = douiContext.getRequest().getServiceInstanceConfig();
			resultValue = serviceInstanceConfig != null ? serviceInstanceConfig.getChannelId() : null;
			if (resultValue == null)
				resultValue = douiContext.getRequest().getPageConfig().getChannelId();
		}
		else if (resultValue.equals("lum_currentUser"))
		{
			resultValue = douiContext.getRequest().getSessionConfig().getUserId();
		}
		else if (resultValue.equals("lum_currentUserName"))
		{
			String userId = douiContext.getRequest().getSessionConfig().getUserId();
			if(!userId.equals(UserConfig.USER_GUEST_ID))
			{
				UserConfig userConfig = ManagerFactory.getUserManager().get(douiContext.getRequest().getSessionConfig(), userId, douiContext.getTransaction());
				resultValue = userConfig.getFullName();
			}
			else
			{
				resultValue = "";
			}
		}
		else if (resultValue.equals("lum_callerInterfaceInstanceId"))
		{
			resultValue = douiContext.getRequest().getCallerServiceInterfaceInstanceId();
		}
		else if (resultValue.equals("lum_callerInterfaceId"))
		{
			String callerInterfaceInstanceId = douiContext.getRequest().getCallerServiceInterfaceInstanceId();
			resultValue = (ManagerFactory.getServiceInterfaceInstanceManager().get(douiContext.getRequest().getSessionConfig(), callerInterfaceInstanceId, douiContext.getTransaction())).getServiceInterfaceId();
		}
		else if (resultValue.equals("lum_currentDateTime"))
		{
			resultValue = new Date();
		}
		else if (resultValue.equals("lum_newGuid"))
		{
			resultValue = PortalUtil.generateNewGuid();
		}

		return resultValue;
	}
}
